<?php

/**
 * llamadaServicio actions.
 *
 * @package    cronos-doctrine
 * @subpackage llamadaServicio
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class llamadaServicioActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->llamada_servicios = Doctrine_Core::getTable('LlamadaServicio')
      ->createQuery('a')
      ->execute();
  }

  public function executeNew(sfWebRequest $request)
  {

         //Recibe la variable IdUser para poder asociarlo al usuario
     if ( ($request->isXmlHttpRequest()) && ($request->getParameter('idUser'))){
        //Busqueda del usuario por el Id
         $usuario = Doctrine_Core::getTable('Usuario')->find(array($request->getParameter('idUser')));
         //Armado del Objeto Mensaje Alerta
         $llamadaServicio = new LlamadaServicio();
         $llamadaServicio->setServicioTipoId($request->getParameter('servicio_id'));
         $llamadaServicio->setUsuario($usuario);

         //Arma Formulario Mensaje Alerta para el usuario en cuestion
         $this->form = new LlamadaServicioForm($llamadaServicio);
         //$this->form->getWidget('usuario_id')->setHidden(FALSE);
         $this->form->setWidget('usuario_id', new sfWidgetFormInputHidden(array(),array('value' => $request->getParameter('idUser'))));
         $this->form->setWidget('servicio_tipo_id', new sfWidgetFormInputHidden(array(),array('value' => $request->getParameter('servicio_id'))));
         //Reasigna la variable usuario para su actualizacion luego Jquery
         $this->idUser = $request->getParameter('idUser');
         $this->parent =  $request->getParameter('parent');
     }
    
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));


    //Captura Variable Id Usuario
    $this->idUser =  $request->getParameter('idUser');
    $this->parent =  $request->getParameter('parent');

    $this->form = new LlamadaServicioForm();

    $this->processForm($request, $this->form, $this->idUser,$this->parent);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($llamada_servicio = Doctrine_Core::getTable('LlamadaServicio')->find(array($request->getParameter('id'))), sprintf('Object llamada_servicio does not exist (%s).', $request->getParameter('id')));
    
    $this->idUser =  $request->getParameter('idUser');
    $this->parent =  $request->getParameter('parent');
    $this->form = new LlamadaServicioForm($llamada_servicio);
    $this->form->setWidget('usuario_id', new sfWidgetFormInputHidden(array(),array('value' => $request->getParameter('idUser'))));
    $this->form->setWidget('servicio_tipo_id', new sfWidgetFormInputHidden(array(),array('value' => $llamada_servicio->getServicioTipoId())));
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($llamada_servicio = Doctrine_Core::getTable('LlamadaServicio')->find(array($request->getParameter('id'))), sprintf('Object llamada_servicio does not exist (%s).', $request->getParameter('id')));
    //Captura Variable Id Usuario
    $this->idUser =  $request->getParameter('idUser');
    $this->parent =  $request->getParameter('parent');
    
    $this->form = new LlamadaServicioForm($llamada_servicio);
    $this->processForm($request, $this->form, $this->idUser,$this->parent);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($llamada_servicio = Doctrine_Core::getTable('LlamadaServicio')->find(array($request->getParameter('id'))), sprintf('Object llamada_servicio does not exist (%s).', $request->getParameter('id')));
    $llamada_servicio->delete();

    $this->redirect('llamadaServicio/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form, $idUser, $parent)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $llamada_servicio = $form->save();

      $this->redirect('llamadaServicio/edit?id='.$llamada_servicio->getId()."&idUser=".$idUser."&parent=".$parent);
    }
  }
}
